package swa2012.telco.provider;

import java.net.URISyntaxException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @author lukas draschkowitz
 * @date 15.06.2012
 */

public class ServiceListener
    implements ServletContextAttributeListener,ServletContextListener  {

  private ServletContext context = null;
  private static TelcoProvider telcoProvider;
  
  public void attributeAdded (ServletContextAttributeEvent scab)
  {
    System.out.println("An attribute was added to the " +
      "ServletContext object");
  }

  public void attributeRemoved (ServletContextAttributeEvent scab)
  {
    System.out.println("An attribute was removed from " +
      "the ServletContext object");
  }

  public void attributeReplaced (ServletContextAttributeEvent scab)
  {
    System.out.println("An attribute was replaced in the " +
      "ServletContext object");
  }


public void contextDestroyed(ServletContextEvent event)
  {

    System.out.println("The Simple Web App. Has Been Removed");
    this.telcoProvider.finalize();
    this.context = null;
   
  }

  public void contextInitialized(ServletContextEvent event)
  {
    this.context = event.getServletContext();
    try {
		this.telcoProvider = new TelcoProvider("12");
	} catch (URISyntaxException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
//    try {
//		
//		System.out.println("telco provider is up");
//		System.out.println("sleeping 5 seconds and then try to register the callback");
//		Thread.sleep(5*1000);
//		Thread t1 = new Thread(new Runnable() {
//			
//			@Override
//			public void run() {
//				try {
//					telcoProvider.registerCallBack("http://student.swa.univie.ac.at:8012/SWA/BrokerCallback");
//				} catch (URISyntaxException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//			}
//		});
//		
//		t1.start();
//		
//	} catch (Exception e) {
//		e.printStackTrace();
//	}
    System.out.println("The Simple Web App. Is Ready");

  }
  
  

}
